package com.jhws.business.comm.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.jhws.common.base.bo.JsonRespData;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.common.MyRequest;
import com.jhws.business.comm.service.ITblAreaDimService;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.sys.menu.service.ITblMenuDimService;
import com.jhws.util.LogHelper;
import org.springframework.web.bind.annotation.ResponseBody;


/* 楼层相关的控制器 */
@Controller
@RequestMapping("/FloorCtrl")
public class FloorCtrl 
{
	@Resource
	protected ITblMenuDimService ITblMenuDimService;
	@Resource
	protected ITblBuildingDimService ITblBuildingDimService;
	@Resource
	protected ITblAreaDimService ITblAreaDimService;

	/* 历史原因，楼层用Unit单词表示了 */
	@RequestMapping("saveBatchs.x")
	@ResponseBody
	public JsonRespData saveBatchFloors(HttpServletRequest request)
			throws java.lang.Exception
	{
	
		/*楼宇套房数量批量生成*/
		String unitNum = MyRequest.GetString(request, "unitNum");
		String roomNum = MyRequest.GetString(request, "roomNum");
		String selectAll = MyRequest.GetString(request, "selectAll");
		String objectList = MyRequest.GetString(request, "objectList");
		String commStr = MyRequest.GetString(request, "commStr");

		if (StringUtils.isNotEmpty(selectAll))//全选所有数据批量生成
		{
			if(StringUtils.isEmpty(commStr)){
				return new JsonRespData("",-1, "小区不能为空");
			}
			String whereStr =  "where comm_code = '" + commStr + "'";
			List<TblBuildingDimBo> allList = ITblBuildingDimService.findTblBuildingDimByWhere(whereStr);
			for (TblBuildingDimBo buildingDimBo : allList) {
				buildingDimBo.setUnitNumber(Integer.valueOf(unitNum));
				buildingDimBo.setRoomNumber(Integer.valueOf(roomNum));
				ITblBuildingDimService.save(buildingDimBo);
			}

		} else if (StringUtils.isNotEmpty(unitNum) && StringUtils.isNotEmpty(objectList) && StringUtils.isNotEmpty(roomNum)) {
			String[] strs = objectList.split("_");
			for (String s : strs) {
				String[] unitInfo = s.split(",");
				if (!commStr.contains(unitInfo[0])) {
					commStr += "'" + unitInfo[0] + "',";
				}

				String whereStr = "where comm_code ='" + unitInfo[0] + "' and building_code='" + unitInfo[1] + "' ";
				List<TblBuildingDimBo> allList = ITblBuildingDimService.findTblBuildingDimByWhere(whereStr);
				for (TblBuildingDimBo buildingDimBo : allList) {
					buildingDimBo.setUnitNumber(Integer.valueOf(unitNum));
					buildingDimBo.setRoomNumber(Integer.valueOf(roomNum));
					ITblBuildingDimService.save(buildingDimBo);
				}
			}
		} else {
			LogHelper.getLogger().debug("-----");
		}

		return new JsonRespData("1",1,"楼层房间生成成功!");

	}


}
